﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace BCData
{
    /// <summary>
    /// SQL 拼接
    /// </summary>
    public class LinkSql
    {
        DynamicParameters dynamicParameters;

        public LinkSql(DynamicParameters dynamicParameters)
        {
            this.dynamicParameters = dynamicParameters;
        }

        /// <summary>
        /// SQL拼接 string 类型
        /// </summary>
        /// <param name="var">变量值</param>
        /// <param name="dbFieldName">变量名(字段名)</param>
        /// <param name="dynamicParameters">SQL参数</param>
        /// <returns></returns>
        public string GetLinkSql(string var, string dbFieldName)
        {
            if (string.IsNullOrWhiteSpace(var))
                return "";
            dynamicParameters.Add("?" + dbFieldName, var, DbType.String);
            return $" AND {dbFieldName} = ?{dbFieldName} ";
        }

        /// <summary>
        /// SQL拼接 枚举类型专用
        /// </summary>
        /// <param name="var">变量值</param>
        /// <param name="dbFieldName">变量名(字段名)</param>
        /// <param name="dynamicParameters">SQL参数</param>
        /// <param name="dbType">参数类型</param>
        /// <returns></returns>
        public string GetLinkSql(string var, string dbFieldName, DbType dbType)
        {
            if (string.IsNullOrWhiteSpace(var))
                return "";

            dynamicParameters.Add("?" + dbFieldName, var, dbType);
            return $" AND {dbFieldName} = ?{dbFieldName} ";
        }

        /// <summary>
        /// SQL拼接  int 类型
        /// </summary>
        /// <param name="var">变量值</param>
        /// <param name="dbFieldName">变量名(字段名)-不用加?</param>
        /// <param name="dynamicParameters">SQL参数</param>
        public string GetLinkSql(int? var, string dbFieldName)
        {
            if (!var.HasValue)
                return "";

            dynamicParameters.Add("?" + dbFieldName, var, DbType.Int64);
            return $" AND {dbFieldName} = ?{dbFieldName} ";
        }

        /// <summary>
        /// SQL拼接 long 类型
        /// </summary>
        /// <param name="var">变量值</param>
        /// <param name="dbFieldName">变量名(字段名)</param>
        /// <param name="dynamicParameters">SQL参数</param>
        public string GetLinkSql(long? var, string dbFieldName)
        {
            if (!var.HasValue)
                return "";

            dynamicParameters.Add("?" + dbFieldName, var, DbType.Int64);
            return $" AND {dbFieldName} = ?{dbFieldName} ";
        }

        /// <summary>
        /// SQL拼接 decimal 类型
        /// </summary>
        /// <param name="var">变量值</param>
        /// <param name="dbFieldName">变量名(字段名)</param>
        /// <param name="dynamicParameters">SQL参数</param>
        public string GetLinkSql(decimal? var, string dbFieldName)
        {
            if (!var.HasValue)
                return "";

            dynamicParameters.Add("?" + dbFieldName, var, DbType.Decimal);
            return $" AND {dbFieldName} = ?{dbFieldName} ";
        }

        /// <summary>
        /// SQL拼接 Date类型
        /// </summary>
        /// <param name="var">变量值</param>
        /// <param name="dbFieldName">变量名(字段名)</param>
        /// <param name="dynamicParameters">SQL参数</param>
        public string GetLinkSql_Date(DateTime? var, string dbFieldName)
        {
            if (!var.HasValue)
                return "";
            dynamicParameters.Add("?" + dbFieldName, var, DbType.Date);
            return $" AND {dbFieldName} = ?{dbFieldName} ";
        }
    }
}
